package com.wes.dsptest.camera;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.wes.dsptest.R;
import com.wes.dsptest.data.TinySharedPreferences;

import java.util.Arrays;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_return;
    private SurfaceView surfaceView;
    private TextureView textureView;
    private Spinner spCameraId;
    private String mCameraId = "0";
    private String mCameraSize = "1920x1080";
    private Spinner spSize;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        initData();
        initView();
        initCamera();
    }


    private void initData() {
        TinySharedPreferences tsp = TinySharedPreferences.instance(this);
        mCameraId = tsp.getStringValue("CameraID", mCameraId);
        mCameraSize = tsp.getStringValue("CameraSize", mCameraSize);
    }

    private void setSaveCameraId(String  id) {
        TinySharedPreferences tsp = TinySharedPreferences.instance(this);
        tsp.setStringValue("CameraID", id);
    }

    private void setSaveCameraSize(String  size) {
        TinySharedPreferences tsp = TinySharedPreferences.instance(this);
        tsp.setStringValue("CameraSize", size);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initCamera() {
        Camera2Wrapper camera2Wrapper = new Camera2Wrapper(this, surfaceView, mCameraId);
        String[] cameraId = camera2Wrapper.getCameraId();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, cameraId);
        adapter.setDropDownViewResource(R.layout.spinner_item_dropdown);
        spCameraId.setAdapter(adapter);
        spCameraId.setSelection(0);

        for(int i = 0; i < cameraId.length-1; i ++)
        {
            if(mCameraId.equals(cameraId[i]))
            {
                spCameraId.setSelection(i);
                break;
            }
        }

        spCameraId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(mCameraId.equals(cameraId[position]))
                    return;
                mCameraId = cameraId[position];
                setSaveCameraId(mCameraId);
                camera2Wrapper.setCameraId(cameraId[position]);
                camera2Wrapper.openCamera(mCameraId);
                Toast.makeText(CameraActivity.this,"摄像头："+cameraId[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });



        String[] cameraSize = camera2Wrapper.convertSizeToString(camera2Wrapper.getCameraSize(mCameraId));
        ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(this, R.layout.spinner_item, cameraSize);
        adapterSize.setDropDownViewResource(R.layout.spinner_item_dropdown);
        spSize.setAdapter(adapterSize);
        spSize.setSelection(0);

        for(int i = 0; i < cameraSize.length - 1 ; i++){
            if(mCameraSize.equals(cameraSize[i]))
            {
                spSize.setSelection(i);
                break;
            }
        }

        spSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if(mCameraSize.equals(cameraSize[position]))
                    return;
                setSaveCameraSize(cameraSize[position]);
                mCameraSize = cameraSize[position];
                camera2Wrapper.setCameraSize(mCameraSize);
                Toast.makeText(CameraActivity.this,"尺寸：："+mCameraSize,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initView() {
        btn_return = findViewById(R.id.btn_return);
        btn_return.setOnClickListener(this);

        surfaceView = findViewById(R.id.sf_view);
        textureView = findViewById(R.id.tt_view);
        spCameraId = findViewById(R.id.sp_cameraId);

        spSize = findViewById(R.id.sp_size);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_return:
//                Intent result = new Intent();
                setResult(RESULT_OK);
                finish();
                break;
        }
    }
}